package com.xiaomi.baidumap.favorite.dao;

import com.j256.ormlite.dao.Dao;
import com.xiaomi.baidumap.favorite.model.LocalRepo;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 16-9-1.
 */
public class LocalRepoDao {

    //处理localrepo的数据库，增添改查
    private Dao<LocalRepo,Long> dao;

    public LocalRepoDao(DBHelper helper) {

        try {
            dao = helper.getDao(LocalRepo.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //增加和刷新数据
    public void createOrUpdate(LocalRepo localRepo){
        try {
            dao.createOrUpdate(localRepo);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //一条一条增加和刷新数据
    public void createOrUpdate(List<LocalRepo> repos){
        for (LocalRepo repo:repos) {
            try {
                dao.createOrUpdate(repo);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //删除数据
    public void deleteData(LocalRepo repo){
        try {
            dao.delete(repo);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //根据本地仓库外键的id,相应的查找到数据
    public List<LocalRepo> queryForGroupId(int groupId){
        try {
            return dao.queryForEq(LocalRepo.GROUP_ID,groupId);
        } catch (SQLException e) {
           throw new RuntimeException(e);
        }
    }
    //查找未分类的数据
    public List<LocalRepo> queryNoGroup(){
        try {
            return dao.queryBuilder().where().isNull(LocalRepo.GROUP_ID).query();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    //查找所有的数据
    public List<LocalRepo> queryAll(){
        try {
            return dao.queryForAll();
        } catch (SQLException e) {
           throw new RuntimeException(e);
        }
    }
}
